package com.boc.langxiang.ui.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.boc.langxiang.R;
import com.boc.langxiang.util.DialogUtil;


/**
 * Created by Administrator on 2015/8/19.
 */
public class MyCustomAlertDialog extends Dialog implements View.OnClickListener, DialogInterface.OnDismissListener {


    private Context context;
    public MyCustomAlertDialog(Context context) {
        super(context, R.style.MyDialogStyle);
        this.context = context;
    }

    private Builder mBuilder;

    @SuppressLint("InflateParams")
    private void create(Builder builder) {
        mBuilder = builder;
        View view = LayoutInflater.from(builder.mContext).inflate(
                R.layout.view_custom_dialog, null);
        TextView titleTextView = (TextView) view.findViewById(R.id.title);
        TextView messageTextView = (TextView) view.findViewById(R.id.message);
        LinearLayout buttonlayout = (LinearLayout) view.findViewById(R.id.button_layout);
        TextView positiveButton = (TextView) view.findViewById(R.id.positive);
        TextView negativeButton = (TextView) view.findViewById(R.id.negative);
        View btnSpaceView = view.findViewById(R.id.view_btn_space);

        if (TextUtils.isEmpty(builder.mTitle)) {
            titleTextView.setVisibility(View.GONE);
        } else {
            titleTextView.setText(builder.mTitle);
        }

        if (TextUtils.isEmpty(builder.mMessage)) {
            messageTextView.setVisibility(View.GONE);
        } else {
            messageTextView.setText(builder.mMessage);
            messageTextView.setGravity(builder.messageGravity);
        }

        if (TextUtils.isEmpty(builder.mPositive) && TextUtils.isEmpty(builder.mNegative)) {
            buttonlayout.setVisibility(View.GONE);
        } else {
            if (TextUtils.isEmpty(builder.mPositive) || TextUtils.isEmpty(builder.mNegative)) {
                btnSpaceView.setVisibility(View.GONE);
            }

            if (TextUtils.isEmpty(builder.mPositive)) {
                positiveButton.setVisibility(View.GONE);
            } else {
                positiveButton.setOnClickListener(this);
                positiveButton.setText(builder.mPositive);
            }

            if (TextUtils.isEmpty(builder.mNegative)) {
                negativeButton.setVisibility(View.GONE);
            } else {
                negativeButton.setOnClickListener(this);
                negativeButton.setText(builder.mNegative);

                if (builder.mNegtiveColor != 0)
                {
                    negativeButton.setTextColor(context.getResources().getColor(builder.mNegtiveColor));
                }
            }
        }

        setOnDismissListener(this);

        setCancelable(builder.mCancelable);

        setContentView(view);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.positive:
                positiveClicked();
                break;
            case R.id.negative:
                negativeClicked();
                break;
            default:
                break;
        }
    }

    private void positiveClicked() {
        dismiss();
        DialogUtil.OnCustomAlertDialogClickListener listenerPos = mBuilder.mListener;
        if (listenerPos != null) {
            listenerPos.onPositiveButtonClick(this);
        }
    }

    private void negativeClicked() {
        dismiss();
        DialogUtil.OnCustomAlertDialogClickListener listenerNeg = mBuilder.mListener;
        if (listenerNeg != null) {
            listenerNeg.onNegativeButtonClick(this);
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
//                negativeClicked();
                break;
            case KeyEvent.KEYCODE_DPAD_CENTER:
//                positiveClicked();
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    public static class Builder {
        private Context mContext;

        public Builder(Context context) {
            mContext = context;
        }

        private String mTitle;
        private String mMessage;
        private int messageGravity = Gravity.CENTER;
        private String mPositive;
        private String mNegative;
        private int mNegtiveColor = 0;

        private boolean mCancelable = true;

        private DialogUtil.OnCustomAlertDialogClickListener mListener;

        public int getmNegtiveColor() {
            return mNegtiveColor;
        }

        public void setmNegtiveColor(int mNegtiveColor) {
            this.mNegtiveColor = mNegtiveColor;
        }

        public Builder setTitle(String title) {
            mTitle = title;
            return this;
        }

        public Builder setTitle(int title) {
            mTitle = mContext.getString(title);
            return this;
        }

        public Builder setMessage(String message) {
            mMessage = message;
            return this;
        }

        public Builder setMessage(int message) {
            mMessage = mContext.getString(message);
            return this;
        }

        /**
         * @param message
         * @param gravity {@link Gravity}
         * @return
         */
        public Builder setMessage(String message, int gravity) {
            mMessage = message;
            messageGravity = gravity;
            return this;
        }

        /**
         * @param message
         * @param gravity {@link Gravity}
         * @return
         */
        public Builder setMessage(int message, int gravity) {
            mMessage = mContext.getString(message);
            messageGravity = gravity;
            return this;
        }

        public Builder setPositive(String positive) {
            mPositive = positive;
            return this;
        }

        public Builder setPositive(int positive) {
            mPositive = mContext.getString(positive);
            return this;
        }

        public Builder setNegative(String negative) {
            mNegative = negative;
            return this;
        }

        public Builder setNegative(int negative) {
            mNegative = mContext.getString(negative);
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            mCancelable = cancelable;
            return this;
        }

        public Builder setOnCustomAlertDialogClickListener(DialogUtil.OnCustomAlertDialogClickListener listener) {
            mListener = listener;
            return this;
        }

        public MyCustomAlertDialog create() {
            MyCustomAlertDialog dialog = new MyCustomAlertDialog(mContext);
            dialog.create(this);
            return dialog;
        }

        public MyCustomAlertDialog show() {
            MyCustomAlertDialog dialog = create();
            dialog.show();
            return dialog;
        }
    }

}